package com.ty.dao.impl;

import com.ty.bean.Grade;
import com.ty.dao.DBUtils;
import com.ty.dao.GradeDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 田野
 */
public class GradeDaoImpl extends DBUtils implements GradeDao {
    @Override
    public List<Grade> getList() {
        /*// 田野啊田野啊，记住这个错，这不报空指针异常才怪 java.lang.NullPointerException
        List<Grade> gradeList = null;*/
        List<Grade> gradeList = new ArrayList<>();

        try {
            String sql = " select * from grade ";
            resultSet = query(sql, null);

            while (resultSet.next()){
                int gradeid = resultSet.getInt("gradeid");
                String gradename = resultSet.getString("gradename");
                Grade grade = new Grade();
                grade.setGradeId(gradeid);
                grade.setGradeName(gradename);
                gradeList.add(grade);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        System.out.println(gradeList);
        return gradeList;
    }
}
